package com.jinzhi.jzweb.controller;


import java.util.*;

import cn.hutool.core.date.DateTime;
import com.jinzhi.jzweb.domain.JazUserCommentDO;
import com.jinzhi.jzweb.domain.JazUserOrderDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazSysContractDO;
import com.jinzhi.jzweb.service.JazSysContractService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 合同管理
 * </pre>
 * <small> 2023-04-18 09:11:44 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazSysContract")
public class JazSysContractController extends BaseController {
	@Autowired
	private JazSysContractService jazSysContractService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazSysContract:jazSysContract")
	String JazSysContract(){
	    return "jzweb/jazSysContract/jazSysContract";
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazSysContract:jazSysContract")
	public Result<Page<JazSysContractDO>> list(JazSysContractDO jazSysContractDTO,String current,String size){
		// 查询列表数据
		Page<JazSysContractDO> page1 = new Page<JazSysContractDO>(Integer.valueOf(current),Integer.valueOf(size));
		Page<JazSysContractDO> page = jazSysContractService.selectPage(page1,
					jazSysContractService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			JazSysContractDO obj = (JazSysContractDO) page.getRecords().get(i);
			obj.setCreateTime(DateTime.of(obj.getCreateTime()));
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}


	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazSysContract:add")
	String add(Model model){
		model.addAttribute("type", "add");
	    return "jzweb/jazSysContract/add";
	}
	

	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazSysContract:add")
	public Result<String> save( JazSysContractDO jazSysContract){
		//设置创建人id、创建时间
		jazSysContract.setSysUserid(getUserId());
		jazSysContract.setCreateTime(new DateTime());
		//状态默认启用
		jazSysContract.setStatus(0);
		jazSysContractService.insert(jazSysContract);
        return Result.ok();
	}


	@GetMapping("/info/{id}")
	@RequiresPermissions("jzweb:jazSysContract:jazSysContract")
	String info(@PathVariable("id") Long id,Model model){
		JazUserOrderDO jazSysContractDO=jazSysContractService.getSysContractById(id);
		model.addAttribute("myData", jazSysContractDO);
		model.addAttribute("type", "info");
		model.addAttribute("pdfList", jazSysContractDO.getContractNamePath());
		model.addAttribute("pdfListYi", jazSysContractDO.getContractNameYiPath());
		return "jzweb/jazSysContract/add";
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazSysContract:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazSysContractDO jazSysContract = jazSysContractService.selectById(id);
		model.addAttribute("jazSysContract", jazSysContract);
		return "jzweb/jazSysContract/edit";
	}
	

	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazSysContract:edit")
	public Result<String>  update( JazSysContractDO jazSysContract){
		jazSysContract.setUpdateTime(new DateTime());
		boolean update = jazSysContractService.updateById(jazSysContract);
		return update ? Result.ok() : Result.fail();
	}
	

	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazSysContract:remove")
	public Result<String>  remove( Long id){
		jazSysContractService.deleteById(id);
        return Result.ok();
	}
	
}
